package com.qzsoft.zhjd.cement.modules.cementmatch.service.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qzsoft.jeemis.common.utils.ConvertDictUtils;
import com.qzsoft.system.modules.sysstation.entity.SysStationEntity;
import com.qzsoft.system.modules.sysstation.enums.SysStationType;
import com.qzsoft.system.modules.sysstation.service.SysStationService;
import com.qzsoft.zhjd.cement.modules.cementdata.entity.CementDataEntity;
import com.qzsoft.zhjd.cement.modules.cementdata.service.CementDataService;
import com.qzsoft.zhjd.cement.modules.cementmatch.dto.CementMatchDetailsDto;
import com.qzsoft.zhjd.cement.modules.cementmatch.dto.CementMatchListDto;
import com.qzsoft.zhjd.cement.modules.cementmatch.entity.CementMatchEntity;
import com.qzsoft.zhjd.cement.modules.cementmatch.mapper.CementMatchMapper;
import com.qzsoft.zhjd.cement.modules.cementmatch.service.CementMatchService;
import com.qzsoft.zhjd.common.annotation.ZhjdDataFilter;
import com.qzsoft.zhjd.common.components.BaseServiceComponent;
import com.qzsoft.zhjd.common.enums.BizCode;
import com.qzsoft.zhjd.common.utils.RenExceptionUtils;

/**
 * @ClassName CementMatchServiceImpl
 * @Description 水稳级配业务接口实现
 * @author linhuawei
 * @since 2022-04-28
 */
@Service
public class CementMatchServiceImpl extends ServiceImpl<CementMatchMapper, CementMatchEntity>
        implements CementMatchService {

    @Autowired
    private CementDataService cementDataService;

    @Autowired
    private SysStationService sysStationService;

    @Autowired
    private BaseServiceComponent baseServiceComponent;

    @Override
    @ZhjdDataFilter(tableAlias = "t1")
    public IPage<CementMatchListDto> pageDto(Map<String, Object> params) {
        IPage<CementMatchListDto> page = baseServiceComponent.getPage(params, CementMatchListDto.class);
        IPage<CementMatchListDto> dataPage = baseMapper.queryDto(page, params);
        return ConvertDictUtils.formatDicDataPage(dataPage);
    }

    @Override
    @ZhjdDataFilter
    public List<CementMatchEntity> list(Map<String, Object> params) {
        QueryWrapper<CementMatchEntity> queryWrapper = new QueryWrapper<CementMatchEntity>();
        if (ObjectUtils.isNotEmpty(params.get("matchCode"))) {
            queryWrapper.eq("match_code", params.get("matchCode").toString());
        }
        if (ObjectUtils.isNotEmpty(params.get("projectId"))) {
            queryWrapper.like("project_id", params.get("projectId").toString());
        }
        if (ObjectUtils.isNotEmpty(params.get("baseCode"))) {
            queryWrapper.like("base_code", params.get("baseCode").toString());
        }
        if (ObjectUtils.isNotEmpty(params.get("stationCode"))) {
            queryWrapper.like("station_code", params.get("stationCode").toString());
        }
        if (ObjectUtils.isNotEmpty(params.get(ZhjdDataFilter.DATA_FILTER_SQL))) {
            queryWrapper.apply(params.get(ZhjdDataFilter.DATA_FILTER_SQL).toString());
        }
        queryWrapper.orderByAsc("station_code,match_code");
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public CementMatchDetailsDto getDetails(String id) {
        CementMatchDetailsDto dto = baseMapper.getDetails(id);
        if (null != dto) {
            return ConvertDictUtils.formatDicData(dto);
        }
        return null;
    }

    @Override
    public CementMatchEntity getByCode(String code) {
        QueryWrapper<CementMatchEntity> queryWrapper = new QueryWrapper<CementMatchEntity>();
        queryWrapper.eq("match_code", code);
        queryWrapper.last(" LIMIT 0,1 ");
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public boolean save(CementMatchEntity entity) {
        // 判断编号是否重复
        QueryWrapper<CementMatchEntity> queryWrapper = new QueryWrapper<CementMatchEntity>();
        queryWrapper.eq("match_code", entity.getMatchCode());
        int count = this.count(queryWrapper);
        if (0 < count) {
            RenExceptionUtils.throwEx(BizCode.PARAMS_ERROR, "配方编号[" + entity.getMatchCode() + "]已经存在");
        }

        // 获取拌和楼
        SysStationEntity station = sysStationService.getByTypeAndCode(SysStationType.CEMENT, entity.getStationCode());
        if (null == station) {
            RenExceptionUtils.throwEx(BizCode.PARAMS_ERROR, "拌和楼不存在");
        }

        // 执行添加操作
        entity.setBaseCode(station.getBaseCode());
        return super.save(entity);
    }

    @Override
    public boolean removeById(Serializable id) {
        // 判断数据是否存在
        CementMatchEntity entity = baseMapper.selectById(id);
        if (null == entity) {
            RenExceptionUtils.throwEx(BizCode.PARAMS_ERROR, "要删除的数据不存在");
        }

        // 判断数据是否在用
        QueryWrapper<CementDataEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("match_code", entity.getMatchCode());
        int count = cementDataService.count(queryWrapper);
        if (0 < count) {
            RenExceptionUtils.throwEx(BizCode.PARAMS_ERROR, "当前配方正在使用，不可删除");
        }

        // 执行删除操作
        return super.removeById(id);
    }

}
